package com.liuwei.msmall.ware.controller;

import com.liuwei.msmall.common.dto.SkuStockTo;
import com.liuwei.msmall.common.enums.BizeEnum;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.R;
import com.liuwei.msmall.ware.entity.WareSkuEntity;
import com.liuwei.msmall.ware.service.WareSkuService;
import com.liuwei.msmall.ware.vo.LockStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品库存
 *
 * @author llww
 * @email 2456888577@qq.com
 * @date 2021-02-25 17:06:18
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;



    //锁定订单对应库存
    @PostMapping("/lock/order")
    public R lockOrderStock(@RequestBody LockStockVo vo){

        try {
            Boolean resps= wareSkuService.lockOrderStock(vo);
            if (!resps){
                return R.error(BizeEnum.PRODUCT_NULL_EXCEPTION.getCode(),BizeEnum.PRODUCT_NULL_EXCEPTION.getMsg());
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(BizeEnum.NO_STOCK_EXCEPTION.getCode(),BizeEnum.NO_STOCK_EXCEPTION.getMsg());
        }

    }


    //根据skuId查看skuids对应是否有库存
    @PostMapping("/hasStock")
    public R getHasStock(@RequestBody List<Long> skuids){
        List<SkuStockTo> skuStockTos= wareSkuService.getHasStock(skuids);
        return R.ok().setData(skuStockTos);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){

		wareSkuService.saveInfo(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
